<template>
	<view class="container">
		<u-form :model="form" ref="uForm" label-position="top" :error-type="['toast']">
			<u-form-item label="反馈类型" prop="complaintObject">
				<u-radio-group v-model="form.complaintObject">
					<u-radio 
						v-for="(item, index) in list" :key="index" 
						:name="item.name"
						:disabled="item.disabled"
					>
						{{item.name}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item label="留言标题" prop="title">
				<u-input v-model="form.title"/>
			</u-form-item>
			<u-form-item label="留言" prop="content">
				<u-input v-model="form.content" type="textarea"/>
			</u-form-item>
			<u-form-item label="照片" prop="date">
				<x-upload
					ref="uUpload"
					:form-data="formData"
					@onUploaded="onUploaded"
					:hasVideo="false"
				/>
			</u-form-item>
		</u-form>
		<u-button class="action-view" @click="saveClick">提交</u-button>
	</view>
</template>

<script>
	import {
		addComplaintRecord
	} from '@/api/common.js'
	import XUpload from '@/components/x-upload'
	import {
		mapState
	} from 'vuex'
	import {
		formatDateByTimestamp
	} from '@/utils/utils.js'
	export default {
		components: {
			XUpload
		},
		data() {
			return {
				form: {
					date: formatDateByTimestamp(Date.now(), 'yyyy/mm/dd hh:MM:ss'),
					content: '',
					complaintObject: '点赞',
				},
				rules: {
					
					title: [{
						required: true,
						message: '请填写反馈标题',
					}, ],
					
					content: [{
						required: true,
						message: '请填写反馈内容',
					}, ],
				},
				
				list: [
					{
						name: '点赞',
						disabled: false
					},
					{
						name: '吐槽',
						disabled: false
					}
				],
				
				formData: {
					'bizType': 'record-feeding'
				},
			}
		},
		onLoad() {
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			saveClick() {
				console.log(this.tchInfo)
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.addRecord();
						return;
					}
				});
			},
			
			addRecord() {
				this.$refs.uUpload.upload();
			},

			onUploaded(data) {
				this.form.files = data.resourceUrl;
				this.form.mediaType = 1;
				const params = { ...this.form }
				if (params.complaintObject === '点赞') {
					params.complaintObject = 'PRAISE'
				} else {
					params.complaintObject = 'ANONYMOUS'
				}
				addComplaintRecord(params).then(res => {
					this.$u.toast('添加成功');
					setTimeout(function(){
						uni.navigateBack({delta:1})
					},1000)
				})
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $app-page-bg-color;
	}
</style>
<style lang="scss" scoped>
	@import '@/css/u-form.scss';
	
	.container {
		padding-left: 30rpx;
		padding-right: 30rpx;
		display: flex;
		flex-direction: column;
	}

	.children-view {
		width: 10rpx;
		flex: 1;
		display: flex;
		flex-direction: row;
	}
</style>
